package all.com.jinyongqunxiax.modules;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import all.com.jinyongqunxiax.R;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.RecyclerView;

import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_DRAGGING;

public class FilterActivity extends AppCompatActivity {
    SearchView   searchView;
    RecyclerView recyclerView;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.filter_menu, menu);

        MenuItem item = menu.findItem(R.id.filter_search_view_item);
        searchView = (SearchView) item.getActionView();
        // searchView.setIconifiedByDefault(false);
        // item.expandActionView();
        // searchView.clearFocus();
        searchView.setQueryHint("输入内容进行过滤");
        // ImageView searchIconView = searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
        // if (searchIconView != null) {
        //    ViewGroup parent = (ViewGroup) searchIconView.getParent();
        //    parent.removeView(searchIconView);
        // }

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.clearFocus();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                filter(newText);
                return false;
            }
        });
        return true;
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (searchView != null) {
            searchView.clearFocus();
        }

        if (recyclerView != null) {
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    if (SCROLL_STATE_DRAGGING == newState) {
                        if (searchView != null) {
                            searchView.clearFocus();
                        }
                    }
                    super.onScrollStateChanged(recyclerView, newState);
                }
            });
        }
    }


    void filter(String text) {

    }

    void loadNewData() {

    }

    void loadMoreData() {

    }
}
